<template>
  <div id="container"></div>
</template>

<script setup>
import { onMounted, onUnmounted } from "vue";
import { Chart } from "@antv/g2";

let chart;
const floatTimestamp = (s) => {
  // +new Date(s) 返回的是当前传入时间的毫秒数，这儿的 +号 等同于date.getTime()、date.valueOf()
  // let date = new Date(s)
  // console.log(+new Date(s))
  // console.log(date.getTime())
  // console.log(date.valueOf())
  // console.log(+date)
  return +new Date(s) + +`0.${s.slice(s.length - 3)}`;
};

const format = (n) => {};

onMounted(() => {
  chart = new Chart({
    container,
    autoFit: true,
  });
  chart
    .title("微妙级区间条形图")
    .interval()
    .coordinate({
        transform: [
            {
                type: 'transpose'
            }
        ]
    })
    .data([
      {
        task: "task0",
        startTime: "2023-06-28 03:30:33.900123", // micro seconds
        endTime: "2023-06-28 03:30:33.900678", // micro seconds
        status: "0",
      },
      {
        task: "task0",
        startTime: "2023-06-28 03:30:33.901123",
        endTime: "2023-06-28 03:30:33.902678",
        status: "1",
      },
    ])
    .encode('x', 'task')
    .encode('y', (d) => floatTimestamp(d.startTime))
    .encode('y1', (d) => floatTimestamp(d.endTime))
    .encode('color', 'status')
    .scale('y', {
        type: 'time',
        domain: [
            new Date('2023-06-28 03:30:33.900'),
            new Date('2023-06-28 03:30:33.903'),
        ]
    })
    .tooltip('y', {
        valueFormatter: (d) => {
            console.log(d)
        }
    })
    .tooltip(['startTime', 'endTime'])

  chart.render();
});

// 销毁
onUnmounted(() => {
  chart.destroy();
  chart = null;
});
</script>

<style lang="less" scoped>
#container {
  width: 800px;
  height: 400px;
}
</style>
